package com.n9.qq.android;

import com.n9.qq.client.FromServiceMsg;
import com.n9.qq.util.BufUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.ByteBuffer;
import java.util.Objects;

/**
 * @author ice
 * @since 2019/2/18 9:54
 */
public class AqFromServiceMsg implements FromServiceMsg {
    private static final Logger LOG = LoggerFactory.getLogger(AqFromServiceMsg.class);

    private int cmd;
    private long qqUin;
    private byte[] bodyData;

    private RespHead respHead;


    public AqFromServiceMsg(int cmd, long qqUin) {
        this.cmd = cmd;
        this.qqUin = qqUin;
    }

    @Override
    public void read(byte[] data) {
        ByteBuffer buf = ByteBuffer.wrap(data);
        byte[] headData = BufUtils.getBytesFromBuf(buf);
        byte[] bodyData = new byte[buf.remaining()];
        buf.get(bodyData);
        this.bodyData = bodyData;

        buf = ByteBuffer.wrap(headData);
        int requestId = buf.getInt();
        buf.position(12);
        String serviceCmd = BufUtils.getStringFromBuf(buf);
        byte[] cookie = BufUtils.getBytesFromBuf(buf);
        this.respHead = new RespHead(requestId, serviceCmd, cookie);
    }

    @Override
    public Integer getId() {
        return Objects.nonNull(this.respHead) ? respHead.getReqId() : null;
    }

    public int getCmd() {
        return cmd;
    }

    public long getQqUin() {
        return qqUin;
    }

    public byte[] getBodyData() {
        return bodyData;
    }

    public RespHead getRespHead() {
        return respHead;
    }
}
